package com.stiff.manage.controller;

import com.github.pagehelper.PageInfo;
import com.stiff.common.bean.EasyUIResult;
import com.stiff.manage.pojo.Content;
import com.stiff.manage.service.ContentService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ContentController.class);
    @Autowired
    private ContentService contentService;
    /**
     * 新增内容
     * @param content
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> save(Content content){
        try {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("【新增内容】content={}",content);
            }
            if(StringUtils.isEmpty(content.getTitle())){
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            content.setId(null);
            this.contentService.save(content);
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("【新增内容】contentId={}",content.getId());
            }
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            LOGGER.error("【新增内容失败！】title="+content.getTitle(),e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 删除内容
     * @param ids
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<Void> deleteContents(@RequestParam(value = "ids")String ids){
        try {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("【删除内容】ids={}", ids);
            }
            List<Object> idList = this.getIdsList(ids);
            if(idList.isEmpty()){
                return ResponseEntity.<Void>badRequest().build();
            }
            this.contentService.deleteByIds(Content.class,"id",idList);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            LOGGER.error("【删除内容失败！】ids=["+ids+"]",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * 查询内容列表
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<EasyUIResult> queryContentList(@RequestParam(value = "categoryId")Long categoryId,
                                                         @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                         @RequestParam(value = "rows",defaultValue = "30")Integer rows){
        try {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("【查询内容列表】page={}，rows={},categoryId={}",page,rows,categoryId);
            }
            EasyUIResult easyUIResult = this.contentService.queryPageList(categoryId,page, rows);
            return ResponseEntity.ok(easyUIResult);
        } catch (Exception e) {
            LOGGER.error("【查询内容失败！】page="+page+",rows="+rows+",categoryId="+categoryId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 修改内容
     * @param content
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> update(Content content){
        try {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("【修改内容】content={}",content);
            }
            if(StringUtils.isEmpty(content.getTitle())){
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            this.contentService.updateSelective(content);
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("【修改内容】contentId={}",content.getId());
            }
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            LOGGER.error("【修改内容失败！】title="+content.getTitle(),e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    private List<Object> getIdsList(String ids){
        List<String> list = Arrays.asList(ids.split(","));
        if (!list.isEmpty()) {
            List<Object> idList = new ArrayList<>();
            for(String str : list) {
                Long i = Long.parseLong(str);
                idList.add(i);
            }
            return idList;
        }
        return null;
    }
}
